This page contains a collection of minimal working examples:
Instructions for compiling and running the examples, whose source code is included in the Java IDL release, are also provided.
The example IDL shown below describes a CORBA object whose single sayHello() operation returns a string.
module HelloApp { interface hello { string sayHello(); }; };
The example server consists of two classes, the servant and the server. The servant, helloServant, is the implementation of the hello IDL interface; each hello instance is implemented by a helloServant instance. The servant is a subclass of _helloImplBase, which is generated by the idltojava compiler from the example IDL. The servant contains one method for each IDL operation, in this example, just the sayHello() method. Servant methods are just like ordinary Java methods; the extra code to deal with the ORB, with marshaling arguments and results, and so on, is provided by the server and the stubs.
The server class has the server's main() method, which:
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; class helloServant extends _helloImplBase { public String sayHello() { return "\nHello world !!\n"; } } public class helloServer { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // create servant and register it with the ORB helloServant helloRef = new helloServant(); orb.connect(helloRef); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // bind the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; ncRef.rebind(path, helloRef); // wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized (sync) { sync.wait(); } } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } }
The example application client below:
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class helloClient { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // resolve the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; hello helloRef = helloHelper.narrow(ncRef.resolve(path)); // call the hello server object and print results String hello = helloRef.sayHello(); System.out.println(hello); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } }
The example applet client below:
// Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import java.awt.Graphics; public class helloApplet extends java.applet.Applet { public void init() { try { // create and initialize the ORB ORB orb = ORB.init(this, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // resolve the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; hello helloRef = helloHelper.narrow(ncRef.resolve(path)); // call the hello server object and print results message = helloRef.sayHello(); } catch (Exception e) { System.out.println("HelloApplet exception: " + e.getMessage()); e.printStackTrace(System.out); } } public void paint(Graphics g) { g.drawString(message, 25, 50); } String message = ""; }
The source code for the examples is located in ${JAVAIDL_HOME}/examples/hello, where JAVAIDL_HOME is the directory in which Java IDL is installed. The following instructions assume you can use port 1050 for the Java IDL name server. Substitute a different port if necessary.
cd ${JAVAIDL_HOME}/examples/hello
idltojava -fclient -fserver hello.idl
javac -classpath \ ${JAVA_HOME}/lib/classes.zip:${JAVAIDL_HOME}/lib/classes.zip:. \ *.java HelloApp/*.java
where JAVA_HOME and JAVAIDL_HOME are where the JDK and Java IDL are installed, respectively.
cd ${JAVAIDL_HOME}/bin nameserv -ORBInitialPort 1050
java -classpath \ ${JAVA_HOME}/lib/classes.zip:${JAVAIDL_HOME}/lib/classes.zip:. \ helloServer -ORBInitialPort 1050
java -classpath \ ${JAVA_HOME}/lib/classes.zip:${JAVAIDL_HOME}/lib/classes.zip:. \ helloClient -ORBInitialPort 1050
Copyright © 1996,1997 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA. 94043-1100 USA., All rights reserved.